# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: Bartłomiej Piotrowski <nospam@bpiotrowski.pl>
pkgname=luajit
_pkgname=luajit2
pkgver=2.1_p20240314
_pkgver=${pkgver/_p/-}
pkgrel=0
_gitrev_tests=9ad3724b1a02855a3cad638bba2e564f825954ce
pkgdesc="OpenResty's branch of LuaJIT"
url="https://github.com/openresty/luajit2"
# ppc64le: fails tests/not really supported
# riscv64 loongarch64: does not build/not supported
arch="all !ppc64le !riscv64 !loongarch64"
license="MIT"
checkdepends="
	perl
	perl-parallel-forkmanager
	sqlite-dev
	zlib-dev
	"
provides="lua"
# lower than 5.1
provider_priority=90
subpackages="$pkgname-dev $pkgname-doc"
source="https://github.com/openresty/luajit2/archive/v$_pkgver/$_pkgname-$_pkgver.tar.gz
	https://github.com/openresty/luajit2-test-suite/archive/$_gitrev_tests/luajit2-test-suite-$_gitrev_tests.tar.gz
	fix-tests-alpine.patch
	module-paths.patch
	"
builddir="$srcdir/$_pkgname-$_pkgver"

prepare() {
	cp -Rl ../luajit2-test-suite-$_gitrev_tests test-suite

	default_prepare

	case "$CARCH" in
		# bit.tobit test failed (got 280328, expected 277312)
		arm*) rm test-suite/test/misc/bit_op.lua;;
		# 35: declaration specifier expected near '_Alignas' at line 882
		x86) rm test-suite/test/sysdep/ffi_include_std.lua;;
	esac
}

build() {
	# It's a language runtime, performance does matter. Users for whom disk
	# size is more important can use lua5.1 instead.
	export CFLAGS="${CFLAGS/-Os/-O2}"

	# These flags are recommended by OpenResty.
	local xcflags="-DLUAJIT_ENABLE_LUA52COMPAT -DLUAJIT_NUMMODE=2"

	make amalg PREFIX=/usr XCFLAGS="$xcflags"
}

check() {
	local prefix="$builddir/test-suite/target"
	mkdir -p "$prefix"

	make install PREFIX="$prefix"

	cd test-suite
	LUA52=1 ./run-tests "$prefix"
}

package() {
	make install DESTDIR="$pkgdir" PREFIX=/usr

	install -Dm644 COPYRIGHT \
		"$pkgdir"/usr/share/licenses/$pkgname/COPYRIGHT
}

sha512sums="
74163706c81f590ebbcd7f8cec9a02ac6ea0896181c7be41e89157e7efd17492bd747ebd9ab3c0c1ff4561110bff1a5fdf68a5e575b36e2d142c76a5c3479ca6  luajit2-2.1-20240314.tar.gz
97026778f76cb4ae42b88e3799fc67eb168dd3a6f0c3837c2aa237b916c550e268dfe7dc8c1a5cc864c28ab0331f3299800abbdcefbe26fc7ba48685bee58001  luajit2-test-suite-9ad3724b1a02855a3cad638bba2e564f825954ce.tar.gz
684be4199634ac6e52183e98b65c9bcdc99217865c47b83a1f579a9f0e1ec55fb1a5fe8ae2f1293ac561c8c806aef269e451e3d73bf168ec20a4fd343c7a89d4  fix-tests-alpine.patch
9c6c410e142838c5128c70570e66beb53cc5422df5ecfb8ab0b5f362637b21ab5978c00f19bc1759165df162fd6438a2ed43e25020400011b7bb14014d87c62e  module-paths.patch
"
